//
//  OrderListViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/6.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OrderListViewController.h"
#import "OrderDetailViewController.h"
#import "HMSegmentedControl.h"
#import "EmployeeOrderDetailVC.h"
#import "CompareDesignOrderDetailVC.h"
#import "DesignSideOrderDetailVC.h"
#import "WaitCommentVC.h"
#import "SelectPayMethodsView.h"

//cell
#import "StoreOrderTableViewCell.h"
#import "DesignOrderTableViewCell.h"

//model
#import "DesignOrderListModel.h"
#import "ShopOrderListModel.h"

@interface OrderListViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>


@property (nonatomic,strong)  HMSegmentedControl  * orderStatusSegMent;
@property (nonatomic, strong) UIScrollView        * tableViewScrollView;//tag    2000
@property (nonatomic, strong)SelectPayMethodsView *selectPayMethodView;

@property (nonatomic, assign) BOOL                isStoreShow;
@property (nonatomic,assign)  BOOL                isMore;
///商城订单的状态
@property (nonatomic,copy)  NSString     *   shop_status;
//nameArr = @[@"全  部",@"待付款",@"待发货",@"待收货",@"待评价",@"已完成",@"售后中",@"售后完成",@"已取消"];1 2 3 4 5 6 7 -1
//nameArr = @[@"全  部",@"待交易",@"待付款",@"竞标中",@"交易中",@"待评价",@"已完成",@"已取消"];nil 10 1 2 3 4 5 -1
///设计订单的状态
@property (nonatomic,copy)  NSString     *   design_status;

///商城订单和设计订单
@property (nonatomic,strong)  ShopOrderListModel    * shopOrderListModel;
@property (nonatomic,strong)  DesignOrderListModel  * designOrderListModel;

@property (nonatomic,strong)  NSMutableArray                   * shopAllListArray;
@property (nonatomic,strong)  NSMutableArray                   * designAllListArray;

//@property (nonatomic,strong)  NSMutableArray            * shopListArray;
//@property (nonatomic,strong)  NSMutableArray            * designListArray;

@property (nonatomic,assign)  NSInteger                 current_page;

@property (nonatomic,assign)  NSInteger                 current_Index;
@property (nonatomic,assign)  NSInteger                 order_id;

//@property (nonatomic,assign)  NSInteger                 current_tableViewIndex;

@end

@implementation OrderListViewController

- (NSMutableArray *)shopAllListArray
{
    if (_shopAllListArray == nil) {
        _shopAllListArray = [[NSMutableArray alloc] init];
    }
    return _shopAllListArray;
}

- (NSMutableArray *)designAllListArray
{
    if (_designAllListArray == nil) {
        _designAllListArray = [[NSMutableArray alloc] init];
    }
    return _designAllListArray;
}

//- (NSMutableArray *)shopListArray
//{
//    if (_shopListArray == nil) {
//        _shopListArray = [[NSMutableArray alloc] init];
//    }
//    return _shopListArray;
//}
//
//- (NSMutableArray *)designListArray
//{
//    if (_designListArray == nil) {
//        _designListArray = [[NSMutableArray alloc] init];
//    }
//    return _designListArray;
//}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self initDefaultData];
    [self createSegMentController];
    [self setSegmentViewUI];
    [self setUI];
    
    [self setSelectPayMethodsView];
    
    
    BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000];
    [tableView.mj_header beginRefreshing];
    
    [self setLeftButton:ImageWithName(@"返回白色") title:nil target:self action:@selector(back)];
}

- (void)back {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)initDefaultData
{
    self.isStoreShow = YES;
    self.current_Index = -1;
//    self.current_tableViewIndex = 0;
    self.shop_status = @"";
    self.design_status = @"";
    self.isMore = NO;
}

#pragma mark - 刷新，请求数据
-(void)setRefreshTableView:(BaseTableView *)tableView
{
    WeakObj(self);
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        selfWeak.current_page = 1;
        if (selfWeak.isStoreShow) {
            [selfWeak requestMallData];
        }else{
            [selfWeak requestDesignData];
        }
    }];
    
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (selfWeak.isStoreShow) {
            [selfWeak requestMallMoreData];
        }else{
            [selfWeak requestDesignMoreData];
        }
    }];
}

- (void)updateDataArray:(BOOL)isMore
{
    if (self.isStoreShow) {
        if (self.shopOrderListModel == nil) {
            return;
        }
    }else{
        if (self.designOrderListModel == nil) {
            return;
        }
    }
    
    if (isMore) {
        if (self.isStoreShow) {
            self.shopAllListArray = [[self.shopAllListArray arrayByAddingObjectsFromArray:self.shopOrderListModel.data] mutableCopy];
        }else{
            self.designAllListArray = [[self.designAllListArray arrayByAddingObjectsFromArray:self.designOrderListModel.data] mutableCopy];
        }
    }else{
        if (self.isStoreShow) {
            self.shopAllListArray = [self.shopOrderListModel.data mutableCopy];
        }else{
            self.designAllListArray = [self.designOrderListModel.data mutableCopy];
        }
    }
}

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
    //我定义了一个 NSInteger tag，是为了记录我当前选择的是分段控件的左边还是右边。
    NSInteger selecIndex = sender.selectedSegmentIndex;
    switch (selecIndex) {
        case 0:
            self.isStoreShow = YES;
            break;
        default:
            self.isStoreShow = NO;
            break;
    }
    
    [self updateSegmentViewWithNameArray];
    [self updateScrollViewSubViews];
    
    self.current_Index = 0;
    [self.orderStatusSegMent setSelectedSegmentIndex:0];
    [self.tableViewScrollView setContentOffset:CGPointMake(0 , 0) animated:NO];
    
    
    BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
    if (self.isStoreShow) {
        if (self.shopAllListArray.count <= 0) {
            [tableView.mj_header beginRefreshing];
        }
    }else{
        if (self.designAllListArray.count <= 0) {
            [tableView.mj_header beginRefreshing];
        }
    }

}


-(void)requestMallData
{
//    nameArr = @[@"全  部",@"待付款",@"待发货",@"待收货",@"待评价",@"已完成",@"售后中",@"售后完成",@"已取消"];
    WeakObj(self);
    self.isMore = NO;
    BaseTableView * tableView = [selfWeak.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
    NSString *url = [NSString stringWithFormat:@"%@app/mall_order_info",REQUESTHEADER];
    NSDictionary *dict = @{};
    if ([self.shop_status isEqualToString:@""]) {
        dict = @{@"page":@1,@"token":[FPLoginAndRegisterTools getToken]};
    }else{
        dict = @{@"page":@1,@"token":[FPLoginAndRegisterTools getToken],@"state":@([self.shop_status integerValue])};
    }
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSDictionary * dict = successResponse;
        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
        [tableView.mj_header endRefreshing];
        if ([status isEqualToString:@"1"]) {
            
            NSError *error;
            selfWeak.shopOrderListModel = [[ShopOrderListModel alloc] initWithDictionary:dict[@"data"] error:&error];
            selfWeak.shopAllListArray = [selfWeak.shopOrderListModel.data mutableCopy];
            [selfWeak updateDataArray:selfWeak.isMore];
            [tableView reloadData];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [tableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}

- (void)requestDesignData
{
    BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
    WeakObj(self);
    self.isMore = NO;
    NSString *url = [NSString stringWithFormat:@"%@app/design_order_info",REQUESTHEADER];
    NSDictionary *dict = @{};
    if ([self.design_status isEqualToString:@""]) {
        dict = @{@"page":@1,@"token":[FPLoginAndRegisterTools getToken]};
    }else{
        dict = @{@"page":@1,@"token":[FPLoginAndRegisterTools getToken],@"state":@([self.design_status integerValue])};
    }
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSDictionary * dict = successResponse;
        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
        [tableView.mj_header endRefreshing];
        if ([status isEqualToString:@"1"]) {
            NSError *error;
            selfWeak.designOrderListModel = [[DesignOrderListModel alloc] initWithDictionary:dict[@"data"] error:&error];
            selfWeak.designAllListArray = [selfWeak.designOrderListModel.data mutableCopy];
            [selfWeak updateDataArray:selfWeak.isMore];
            [tableView reloadData];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        
        [tableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}

- (void)requestDesignMoreData
{
    self.current_page ++;
    self.isMore = YES;
    NSString *url = [NSString stringWithFormat:@"%@app/design_order_info",REQUESTHEADER];
    NSDictionary *dict = @{@"page":@(self.current_page),@"token":[FPLoginAndRegisterTools getToken],@"state":@([self.design_status integerValue])};
    
    BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
    
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        
        NSDictionary * dict = successResponse;
        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
        
        [tableView.mj_footer endRefreshing];
        
        if ([status isEqualToString:@"1"]) {
            
            selfWeak.current_page = [successResponse[@"data"][@"last_page"] integerValue];
            
            NSError * error;
            selfWeak.designOrderListModel = [[DesignOrderListModel alloc] initWithDictionary:dict[@"data"] error:&error];
            selfWeak.designAllListArray = [[selfWeak.designAllListArray arrayByAddingObjectsFromArray:selfWeak.designOrderListModel.data] mutableCopy];
            [selfWeak updateDataArray:selfWeak.isMore];
            [tableView reloadData];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [tableView.mj_footer endRefreshing];
        [HttpManager showFail];
    }];
}

-(void)requestMallMoreData
{
    self.current_page ++;
    self.isMore = YES;
    NSString *url = [NSString stringWithFormat:@"%@app/mall_order_info",REQUESTHEADER];
    NSDictionary *dict = @{@"page":@(self.current_page),@"token":[FPLoginAndRegisterTools getToken],@"state":@([self.design_status integerValue])};
    BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
    
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSDictionary * dict = successResponse;
        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
        [tableView.mj_footer endRefreshing];
        if ([status isEqualToString:@"1"]) {
           
            selfWeak.current_page = [successResponse[@"data"][@"last_page"] integerValue];
            NSError * error;
            selfWeak.shopOrderListModel = [[ShopOrderListModel alloc] initWithDictionary:dict[@"data"] error:&error];
            selfWeak.shopAllListArray = [[selfWeak.shopAllListArray arrayByAddingObjectsFromArray:selfWeak.shopOrderListModel.data] mutableCopy];
            [selfWeak updateDataArray:selfWeak.isMore];
            [tableView reloadData];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [tableView.mj_footer endRefreshing];
        [HttpManager showFail];
    }];
}

-(void)createSegMentController{
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"商城订单",nil];//,@"设计订单"
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    segmentedControl.frame = CGRectMake(0, 0, 180, 30);
    
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = [UIColor colorWithRed:252/255.0 green:245/255.0 blue:248/255.0 alpha:1];
    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    
//    [self.navigationItem setTitleView:segmentedControl];
}

-(void)setSelectPayMethodsView{
    self.selectPayMethodView = [[SelectPayMethodsView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //    self.selectPayMethodView.price.text = _total_price
    __weak __typeof(self) weakSelf = self;
    self.selectPayMethodView.hiddenClick = ^{
        [weakSelf hiddenSelectPayMethodsView];
    };
    
    self.selectPayMethodView.doneClick = ^{
        if (weakSelf.selectPayMethodView.is_bankCard) {
            [weakSelf pay_by_walletWithOrder_id:weakSelf.order_id];
        }
    };
    [kKeyWindow addSubview:self.selectPayMethodView];
}

-(void)pay_by_walletWithOrder_id:(NSInteger)order_id
{
    NSString * url = [NSString stringWithFormat:@"%@app/order_pay_by_wallet",REQUESTHEADER];
    NSDictionary * parameter = @{@"token":kNSUDefaultReadKey(@"token"),@"order_id":@(order_id),@"password":@"123456"};
    [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
        [HttpManager hideWait];
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:@"支付成功"];
            [UIView animateWithDuration:1.0 animations:^{
                
            } completion:^(BOOL finished) {
                [self hiddenSelectPayMethodsView];

            }];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
        [tableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}

- (void)showSelectPayMethodsViewWithOrder_id:(NSInteger)order_id
{
    WeakObj(self);
    [UIView animateWithDuration:0.3 animations:^{
        self.selectPayMethodView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.selectPayMethodView.doneClick = ^{
            if (selfWeak.selectPayMethodView.is_bankCard) {
                [selfWeak pay_by_walletWithOrder_id:order_id];
            }
        };
    } completion:^(BOOL finished) {
        self.selectPayMethodView.shadowBtn.alpha = 1;
        
    }];
}

- (void)hiddenSelectPayMethodsView{
    self.selectPayMethodView.shadowBtn.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.selectPayMethodView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
}

-(void)setSegmentViewUI
{
    self.orderStatusSegMent = [[HMSegmentedControl alloc] init];
    [self updateSegmentViewWithNameArray];
    self.orderStatusSegMent.frame = CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(30));
    self.orderStatusSegMent.backgroundColor = RGB_COLOR(235, 67, 90);
    self.orderStatusSegMent.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.orderStatusSegMent.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
    self.orderStatusSegMent.titleTextAttributes = @{NSForegroundColorAttributeName:RGBACOLOR(255, 255, 255, 0.6),NSFontAttributeName:[UIFont systemFontOfSize:16]};
    self.orderStatusSegMent.selectedTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
    self.orderStatusSegMent.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.orderStatusSegMent.selectionIndicatorHeight = 2;
    self.orderStatusSegMent.selectionIndicatorColor = [UIColor whiteColor];
    //设置开始时默认选中的下标
    self.orderStatusSegMent.selectedSegmentIndex = 0;
    //设置选中的选项卡的点击事件
    [self.orderStatusSegMent addTarget:self action:@selector(segmentDidChangedValue:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.orderStatusSegMent];
}
- (void)updateSegmentViewWithNameArray
{
    NSArray *nameArr = @[];
    if (self.isStoreShow) {
        nameArr = @[@"全  部",@"待付款",@"待发货",@"待收货",@"待评价",@"已完成",@"售后中",@"售后完成",@"已取消"];
    }else{
        nameArr = @[@"全  部",@"待交易",@"待付款",@"竞标中",@"交易中",@"待评价",@"已完成",@"已取消"];
    }
    self.orderStatusSegMent.sectionTitles = nameArr;
}

-(void)segmentDidChangedValue:(HMSegmentedControl *)segmentedControl
{
    [self.tableViewScrollView setContentOffset:CGPointMake(SCREEN_WIDTH*segmentedControl.selectedSegmentIndex  , 0) animated:true];
    if (self.orderStatusSegMent == segmentedControl) {
        BaseTableView * tableView = [self.tableViewScrollView viewWithTag:2000 + self.orderStatusSegMent.selectedSegmentIndex];
        [tableView.mj_header beginRefreshing];
        [tableView reloadData];
        NSLog(@"segmentIndex --- %ld", (long)segmentedControl.selectedSegmentIndex);
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableViewScrollView) {
        NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
        
        if (index != self.current_Index) {
            self.orderStatusSegMent.selectedSegmentIndex = index;
            self.current_Index = index;
            NSLog(@"scrollViewIndex --- %ld", (long)index);
            BaseTableView * tableView = [self.tableViewScrollView viewWithTag:index + 2000];
            if (self.isStoreShow) {
                if (index == 0) {
                    self.shop_status = @"";
                }else if(index == 8){
                    self.shop_status = @"-1";
                }else{
                    if (index != 8 && index != 0) {
                        self.shop_status = [NSString stringWithFormat:@"%ld",(long)index];
                    }
                }
            }else{
                if (index == 0) {
                    self.design_status = @"";
                }
                if (index == 1) {
                    self.design_status = @"10";
                }
                if (index == 2) {
                    self.design_status = @"1";
                }
                if (index == 3) {
                    self.design_status = @"2";
                }
                if (index == 4) {
                    self.design_status = @"3";
                }
                if (index == 5) {
                    self.design_status = @"4";
                }
                if (index == 6) {
                    self.design_status = @"5";
                }
                if (index == 7) {
                    self.design_status = @"-1";
                }
                
            }
            
            [self updateDataArray:_isMore];
            [tableView reloadData];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.tableViewScrollView) {
        NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
        
        BaseTableView * tableView = [self.tableViewScrollView viewWithTag:index + 2000];
        [tableView.mj_header beginRefreshing];
    }
}

-(void)uploadCancelDataWithOrder_id:(NSInteger)order_id
{
    NSString *url = [NSString stringWithFormat:@"%@app/order_cancel",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken],@"id":@(order_id),@"state":@(0)};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:@"已取消订单"];
            BaseTableView * tableView = [self.tableViewScrollView viewWithTag:self.orderStatusSegMent.selectedSegmentIndex + 2000];
            [tableView.mj_header beginRefreshing];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
        
    }];
}

-(void)uploadReciptDataWithOrder_id:(NSInteger)order_id
{
    NSString *url = [NSString stringWithFormat:@"%@app/order_cancel",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken],@"id":@(order_id),@"state":@(4)};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:@"已确认收货"];
            BaseTableView * tableView = [self.tableViewScrollView viewWithTag:self.orderStatusSegMent.selectedSegmentIndex + 2000];
            [tableView.mj_header beginRefreshing];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
        
    }];
}

- (void)setUI {
    self.tableViewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenX375(30), SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(30))];
    self.tableViewScrollView.showsVerticalScrollIndicator = false;
    self.tableViewScrollView.showsHorizontalScrollIndicator = false;
    self.tableViewScrollView.pagingEnabled = true;
    self.tableViewScrollView.delegate = self;
    if (_isStoreShow) {
        self.tableViewScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 9, 0);
    }
    self.tableViewScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 8, 0);
    self.tableViewScrollView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    [self.view addSubview:self.tableViewScrollView];
    
    [self updateScrollViewSubViews];
}


- (void)updateScrollViewSubViews
{
    int count = 8;
    if (self.isStoreShow) {
        count = 9;
    }
    
    [self.tableViewScrollView removeAllSubviews];
    
    for (int i = 0; i < count; i++) {
        BaseTableView * tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(30))];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.tag = 2000 + i;

        [self setRefreshTableView:tableView];

        [self.tableViewScrollView addSubview:tableView];
    }
}

#pragma mark - UITableViewDelegate,DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.isStoreShow) {
//        if (self.orderStatusSegMent.selectedSegmentIndex == 0) {
            return self.shopAllListArray.count;
//        }
//        return self.shopListArray.count;
    }
    
    return self.designAllListArray.count;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma 弹出提示框
- (void)showAlert:(NSString *)order_statusStr design_id:(NSInteger)design_id
{
//    if (self.shopOrderListModel.data == nil) {
//        return;
//    }
//    if (self.designOrderListModel.data == nil) {
//        return;
//    }
    //显示提示框
    UIAlertController* alert = [[UIAlertController alloc] init];
    if (self.isStoreShow) {
        if ([order_statusStr isEqualToString:@"1"]) {//取消订单
            alert = [UIAlertController alertControllerWithTitle:nil  message:@"确认取消订单？"  preferredStyle:UIAlertControllerStyleAlert];
        }
        if ([order_statusStr isEqualToString:@"2"]) {//待发货
            alert = [UIAlertController alertControllerWithTitle:nil  message:@"确认取消订单？"  preferredStyle:UIAlertControllerStyleAlert];
        }
        if ([order_statusStr isEqualToString:@"3"]) {//确认收货
            alert = [UIAlertController alertControllerWithTitle:nil  message:@"确认收货？"  preferredStyle:UIAlertControllerStyleAlert];
        }
    }else{
        if ([order_statusStr isEqualToString:@"10"]) {//待交易
            alert = [UIAlertController alertControllerWithTitle:nil  message:@"确认取消订单？"  preferredStyle:UIAlertControllerStyleAlert];
        }
        if ([order_statusStr isEqualToString:@"2"]) {//竞标中
            alert = [UIAlertController alertControllerWithTitle:nil  message:@"确认取消订单？"  preferredStyle:UIAlertControllerStyleAlert];
        }
        if ([order_statusStr isEqualToString:@"3"]) {//交易中
            alert = [UIAlertController alertControllerWithTitle:nil  message:@"确认交易？"  preferredStyle:UIAlertControllerStyleAlert];
        }
        
    }
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault   handler:^(UIAlertAction * action) {
        //响应事件
        if (self.isStoreShow) {
            if ([order_statusStr isEqualToString:@"1"]) {
                [self uploadCancelDataWithOrder_id:design_id];
//                [self shopOrderDetailWithDesign_id:design_id withOrder_status:@"0"];
            }
            if ([order_statusStr isEqualToString:@"2"]) {
                [self uploadCancelDataWithOrder_id:design_id];
//                [self shopOrderDetailWithDesign_id:design_id withOrder_status:@"0"];
            }
            if ([order_statusStr isEqualToString:@"3"]) {
                [self uploadReciptDataWithOrder_id:design_id];
//                [self shopOrderDetailWithDesign_id:design_id withOrder_status:@"4"];
            }
        }else{
            if ([order_statusStr isEqualToString:@"10"]) {//待交易
//                [self shopOrderDetailWithDesign_id:design_id withOrder_status:@"0" withOrder_no:];
            }
            if ([order_statusStr isEqualToString:@"2"]) {//竞标中
                [self employeeOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
            }
            if ([order_statusStr isEqualToString:@"3"]) {//交易中
                [self compareOrderDetailWithDesign_id:design_id withDesign_status:@"order_statusStr"];
            }
        }
    }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel   handler:^(UIAlertAction * action) {
        //响应事件
        NSLog(@"action = %@", action);
    }];
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isStoreShow) {
        StoreOrderTableViewCell *cell = [StoreOrderTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        NSInteger design_id = [[self.shopOrderListModel.data valueForKey:@"Id"][indexPath.section] integerValue];
        NSString *order_statusStr = [self.shopOrderListModel.data valueForKey:@"order_status"][indexPath.section];
        NSInteger order_no = [[self.shopOrderListModel.data valueForKey:@"order_no"][indexPath.section] integerValue];
        self.order_id = design_id;

        cell.leftClick = ^{
            
            if ([order_statusStr isEqualToString:@"1"]) {//取消订单
                [self showAlert:order_statusStr design_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"2"]) {//取消订单
                [self showAlert:order_statusStr design_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"3"]) {//确认收货
                [self showAlert:order_statusStr design_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"4"]) {//申请售后
                [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr withOrder_no:order_no];
            }
        };
        
        cell.rightClick = ^{
            if ([order_statusStr isEqualToString:@"0"]) {//查看详情
                [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr withOrder_no:order_no];
            }
            if ([order_statusStr isEqualToString:@"1"]) {//立即付款
                [self showSelectPayMethodsViewWithOrder_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"2"]) {//提醒卖家发货
                [HttpManager showNoteMsg:@"已提醒卖家发货"];
            }
            if ([order_statusStr isEqualToString:@"3"]) {//查看物流
                [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr withOrder_no:order_no];
            }
            if ([order_statusStr isEqualToString:@"4"]) {//立即评价
                WaitCommentVC *vc = [[WaitCommentVC alloc] init];
                vc.order_id = design_id;
                vc.order_no = [[self.shopOrderListModel.data valueForKey:@"order_no"][indexPath.section] integerValue];
                vc.type = @"商城订单详情";
                [self.navigationController pushViewController:vc animated:true];
            }
            if ([order_statusStr isEqualToString:@"5"]) {//申请售后
                [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr withOrder_no:order_no];
            }
        };
        
        cell.shopOrderListDataModel = self.shopAllListArray[indexPath.section];

        return cell;
    }else {
        NSInteger design_id = [[self.designOrderListModel.data valueForKey:@"Id"][indexPath.section] integerValue];
        NSString *order_statusStr = [self.designOrderListModel.data valueForKey:@"order_status"][indexPath.section];
        NSInteger order_no = [[self.designOrderListModel.data valueForKey:@"order_no"][indexPath.section] integerValue];
        DesignOrderTableViewCell *cell = [DesignOrderTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.btnClick = ^{
            if ([order_statusStr isEqualToString:@"0"]) {//取消订单
                [self showAlert:order_statusStr design_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"1"]) {//待付款
                
            }
            if ([order_statusStr isEqualToString:@"2"]) {//竞标中
                [self showAlert:order_statusStr design_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"3"]) {//交易中
                [self showAlert:order_statusStr design_id:design_id];
            }
            if ([order_statusStr isEqualToString:@"4"]) {//待评价
                WaitCommentVC *vc = [[WaitCommentVC alloc] init];
                vc.type = @"设计订单详情";
                vc.order_id = design_id;
                vc.order_status = order_statusStr;
                vc.order_no = order_no;
                [self.navigationController pushViewController:vc animated:true];
            }
            if ([order_statusStr isEqualToString:@"5"]) {//已完成
                
            }
            if ([order_statusStr isEqualToString:@"10"]) {//待交易
                
            }
        };
        cell.designOrderListDataModel = self.designAllListArray[indexPath.section];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isStoreShow) {
        NSInteger design_id = [[self.shopOrderListModel.data valueForKey:@"Id"][indexPath.section] integerValue];
        NSString *order_statusStr = [self.shopOrderListModel.data valueForKey:@"order_status"][indexPath.section];
        NSInteger order_no = [[self.shopOrderListModel.data valueForKey:@"order_no"][indexPath.section] integerValue];
//        if ([order_statusStr isEqualToString:@"0"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"1"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"2"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"3"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"4"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"5"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"6"]) {
//            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr];
//        }
//        if ([order_statusStr isEqualToString:@"7"]) {
            [self shopOrderDetailWithDesign_id:design_id withOrder_status:order_statusStr withOrder_no:order_no];
//        }
        
    }else{
        NSInteger design_id = [[self.designOrderListModel.data valueForKey:@"Id"][indexPath.section] integerValue];
        NSString *order_statusStr = [self.designOrderListModel.data valueForKey:@"order_status"][indexPath.section];

//        if ([order_statusStr isEqualToString:@"0"]) {//已取消
//            [self employeeOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
//        }
        if ([order_statusStr isEqualToString:@"1"]) {//待付款
            [self employeeOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
        }
        if ([order_statusStr isEqualToString:@"2"]) {//竞标中
            [self employeeOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
        }
        if ([order_statusStr isEqualToString:@"3"]) {//交易中
            [self compareOrderDetailWithDesign_id:design_id withDesign_status:@"order_statusStr"];
        }
        if ([order_statusStr isEqualToString:@"4"]) {//待评价
            [self compareOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
        }
        if ([order_statusStr isEqualToString:@"5"]) {//已完成
            [self compareOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
        }
//        if ([order_statusStr isEqualToString:@"10"]) {//待交易
//            [self compareOrderDetailWithDesign_id:design_id withDesign_status:order_statusStr];
//        }
    }
}

-(void)shopOrderDetailWithDesign_id:(NSInteger)design_id withOrder_status:(NSString *)order_statusStr withOrder_no:(NSInteger)order_no
{
    OrderDetailViewController *vc = [[OrderDetailViewController alloc] init];
    vc.order_id = design_id;
    vc.order_status = order_statusStr;
    vc.order_no = order_no;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)employeeOrderDetailWithDesign_id:(NSInteger)design_id withDesign_status:(NSString *)design_status
{
    EmployeeOrderDetailVC *vc = [[EmployeeOrderDetailVC alloc] init];
    vc.type = @"设计订单详情";
    vc.design_id = design_id;
    vc.design_status = design_status;
    [self.navigationController pushViewController:vc animated:true];
}

-(void)compareOrderDetailWithDesign_id:(NSInteger)design_id withDesign_status:(NSString *)design_status
{
    CompareDesignOrderDetailVC *vc = [[CompareDesignOrderDetailVC alloc] init];
    vc.type = @"设计订单详情";
    vc.design_id = design_id;
    vc.design_status = design_status;
    [self.navigationController pushViewController:vc animated:true];
}

-(void)designSideOrderDetailWithDesign_id:(NSInteger)design_id withDesign_status:(NSString *)design_status
{
    DesignSideOrderDetailVC *vc = [[DesignSideOrderDetailVC alloc] init];
    vc.type = @"设计订单详情";
    vc.design_id = design_id;
    vc.design_status = design_status;
    [self.navigationController pushViewController:vc animated:true];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isStoreShow) {
        return ScreenX375(190);
    }
    return ScreenX375(160);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return ScreenX375(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(10))];
    view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    return view;
}


/*
 if ([order_statusStr isEqualToString:@"0"]) {//已取消
 
 }
 if ([order_statusStr isEqualToString:@"1"]) {//待付款
 
 }
 if ([order_statusStr isEqualToString:@"2"]) {//竞标中
 
 }
 if ([order_statusStr isEqualToString:@"3"]) {//交易中
 
 }
 if ([order_statusStr isEqualToString:@"4"]) {//待评价
 
 }
 if ([order_statusStr isEqualToString:@"5"]) {//已完成
 
 }
 if ([order_statusStr isEqualToString:@"10"]) {//待交易
 
 }
 */
@end
